$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'qyxxdd/list',
        datatype: "json",
        colModel: [
            { label: 'id', name: 'id', index: "id", width: 45, key: true,hidden:true },
            { label: '查看详情', name: 'ddxq', index: 'ddxq', width: 80,
                formatter:function (cellvale, options, rowObject){
                    return "<a class='btn btn-modify'  onclick='vm.detail(\"" + rowObject.id + "\", \"" + rowObject.deptId + "\")' style='display: inline;'>详情</a>";
                }
            },
			{ label: '所属分局', name: 'ddSsfj', index: 'DD_SSFJ', width: 80,hidden:true  },
			{ label: '所属派出所', name: 'ddSspcs', index: 'DD_SSPCS', width: 80,hidden:true  },
			{ label: '企业代码', name: 'ddQydm', index: 'DD_QYDM', width: 80 }, 
			{ label: '企业名称', name: 'ddQymc', index: 'DD_QYMC', width: 80 }, 
			{ label: '组织机构代码', name: 'ddZzjgdm', index: 'DD_ZZJGDM', width: 80 }, 
			{ label: '经营地址', name: 'ddJydz', index: 'DD_JYDZ', width: 80 }, 
			{ label: '联系电话', name: 'ddLxdh', index: 'DD_LXDH', width: 80 }, 
			{ label: '传真', name: 'ddCz', index: 'DD_CZ', width: 80,hidden:true   },
			{ label: '邮政编码', name: 'ddYzbm', index: 'DD_YZBM', width: 80 }, 
			{ label: '经济类型代码', name: 'ddJjlxdm', index: 'DD_JJLXDM', width: 80  ,hidden:true},
			{ label: '法定代表人姓名', name: 'ddFddbrxm', index: 'DD_FDDBRXM', width: 80 }, 
			{ label: '股东信息', name: 'ddGdxx', index: 'DD_GDXX', width: 80 ,hidden:true },
			{ label: '经营范围', name: 'ddJyfw', index: 'DD_JYFW', width: 80 ,hidden:true },
			{ label: '营业执照号', name: 'ddYyzzh', index: 'DD_YYZZH', width: 80,hidden:true  },
			{ label: '营业执照发证机构名称', name: 'ddYyzzfzjgmc', index: 'DD_YYZZFZJGMC', width: 80,hidden:true  },
			{ label: '登记日期', name: 'ddDjrq', index: 'DD_DJRQ', width: 80 },
			{ label: '治安管理机构代码', name: 'ddZagljgdm', index: 'DD_ZAGLJGDM', width: 80,hidden:true  },
			{ label: '企业状态代码', name: 'ddQyztdm', index: 'DD_QYZTDM', width: 80,hidden:true  },
			{ label: '典当经营许可证编号', name: 'ddDdjyxkzbh', index: 'DD_DDJYXKZBH', width: 80,hidden:true  },
			{ label: '典当经营许可证发证机构名称', name: 'ddDdjyxkzfzjgmc', index: 'DD_DDJYXKZFZJGMC', width: 80,hidden:true  },
			{ label: '经营项目', name: 'ddJyxm', index: 'DD_JYXM', width: 80 ,hidden:true },
			{ label: '备案日期', name: 'ddBarq', index: 'DD_BARQ', width: 80,hidden:true  },
			{ label: '是否安装软件', name: 'ddSfazrj', index: 'DD_SFAZRJ', width: 80,hidden:true  },
			{ label: '软件安装日期', name: 'ddRjazrq', index: 'DD_RJAZRQ', width: 80 ,hidden:true },
			{ label: '软件未安装原因代码', name: 'ddRjWazyyCode', index: 'DD_RJ_WAZYY_CODE', width: 80 ,hidden:true },
			{ label: '是否安装读卡器', name: 'ddSfazdkq', index: 'DD_SFAZDKQ', width: 80  ,hidden:true},
			{ label: '读卡器安装日期', name: 'ddDkqazrq', index: 'DD_DKQAZRQ', width: 80  ,hidden:true},
			{ label: '读卡器未安装原因代码', name: 'ddDkqWazyyCode', index: 'DD_DKQ_WAZYY_CODE', width: 80 ,hidden:true },
			{ label: '是否接入监控平台', name: 'ddSfjrjkpt', index: 'DD_SFJRJKPT', width: 80  ,hidden:true},
			{ label: '平台接入日期', name: 'ddPtjrrq', index: 'DD_PTJRRQ', width: 80  ,hidden:true},
			{ label: '视频监控未安装原因代码', name: 'ddSpjkWazyyCode', index: 'DD_SPJK_WAZYY_CODE', width: 80 ,hidden:true },
			{ label: '是否安装硬盘', name: 'ddSfazyp', index: 'DD_SFAZYP' ,hidden:true, width: 80 ,formatter: function(value, options, row){
                var str="";
                for(var i in vm.ddSfazyp){
                    if(vm.ddSfazyp[i].value===value){
                        str=vm.ddSfazyp[i].text;
                    }
                }
                return  str;
            }},
			{ label: '硬盘安装日期', name: 'ddYpazrq', index: 'DD_YPAZRQ', width: 80 ,hidden:true },
			{ label: '', name: 'ddThzbh', index: 'DD_THZBH', width: 80 ,hidden:true }
        ],
		viewrecords: true,
        height: 365,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});
var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};
var ztree;
var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{

        },
		showList: true,
        showDetail: false,
		title: null,
        qyxxDd:{
            parentName:null,
            parentId:0,
            orderNum:0
        },
        ddSfazrj:[{text:'是',value:'是'},{text:'否',value:'否'}],
        ddSfazyp:[{text:'是',value:'是'},{text:'否',value:'否'}],
        ddSfazdkq:[{text:'是',value:'是'},{text:'否',value:'否'}],
        ddSfjrjkpt:[{text:'是',value:'是'},{text:'否',value:'否'}],
	},
    mounted:{

    },
	methods: {
        getDept: function(){
            //加载部门树
            $.get(baseURL + "sys/dept/select", function(r){
                ztree = $.fn.zTree.init($("#deptTree"), setting, r.deptList);
                var node = ztree.getNodeByParam("deptId", vm.dept.parentId);
                ztree.selectNode(node);
                vm.dept.parentName = node.name;

            })
        },

		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
            sfsofty:true;
            sfsoftn:true;
			vm.title = "新增";
            vm.qyxxDd = { ddSspcsMc:null,ddSspcs:0,orderNum:0};

            vm.getDept();
		},

		update: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.getDept();
			vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id);
		},
		saveOrUpdate: function (event) {
            if ( vm.qyxxDd.ddQydm ==null) {
                alert("企业代码不能为空！");
                return false;
            }
            if ( vm.qyxxDd.ddQymc ==null) {
                alert("企业名称不能为空！");
                return false;
            }
            vm.qyxxDd.ddDjrq=$("#djDate").val();
            vm.qyxxDd.ddBarq=$("#baDate").val();
            vm.qyxxDd.ddRjazrq=$("#rjDate").val();
            vm.qyxxDd.ddDkqazrq=$("#dkqDate").val();
            vm.qyxxDd.ddPtjrrq=$("#ptDate").val();
            vm.qyxxDd.ddYpazrq=$("#ypDate").val();
			var url = vm.qyxxDd.id == null ? "qyxxdd/save" : "qyxxdd/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.qyxxDd),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "qyxxdd/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "qyxxdd/info/"+id, function(r){
                vm.qyxxDd = r.qyxxDd;
            });
		},
        getUser: function(id){
            $.get(baseURL + "sys/user/info/"+id, function(r){
                vm.user = r.user;
                console.log("user:",r.user);
                vm.user.password = null;
                vm.getDept();
            });
        },
        detail: function (id) {
            vm.showDetail = true;
            vm.getInfo(id);
        },
        deptTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择部门",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#deptLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级部门
                    vm.qyxxDd.ddSspcs = node[0].deptId;
                    vm.qyxxDd.ddSspcsMc = node[0].name;
                    // if(vm.qyxxDd.parentId==0){
                    //     $(".dbmbh").css("display","none");
                    //     $(".dbmbh").html("");
                    // }else{
                    //     $(".dbmbh").css("display","block");
                    //     $(".dbmbh").html(node[0].deptCode);
                    // }
                    layer.close(index);

                }
            });
        },
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
                postData:{'name':vm.q.name}
            }).trigger("reloadGrid");
		}
	}
});
var Dept = {
    id: "deptTable",
    table: null,
    layerIndex: -1
};
function getDeptId () {
    var selected = $('#deptTable').bootstrapTreeTable('getSelections');
    if (selected.length == 0) {
        alert("请选择一条记录");
        return false;
    } else {
        return selected[0].id;
    }
}

$(function () {
    $.get(baseURL + "sys/dept/info", function(r){
        var colunms = Dept.initColumn();
        var table = new TreeTable(Dept.id, baseURL + "sys/dept/list", colunms);
        table.setRootCodeValue(r.deptId);
        table.setExpandColumn(1);
        table.setIdField("deptId");
        table.setCodeField("deptId");
        table.setParentCodeField("parentId");
        table.setExpandAll(false);
        table.init();
        Dept.table = table;
    });
    $('input[type=radio][name=ddSfazrj]').change(function() {


    });
});

